<template>
    <view class="label-main">
        <labelList @update:updateUserTagFn="ev => state.checkLabel = ev" :isPage="true" :tagsList="state.tagsList" ref="PersonalLabelRef"/>
        <van-button type="primary" :loading="state.isLoading" @click="nextFn()">下一步</van-button>
    </view>
</template>

<script setup>
import { reactive, ref, getCurrentInstance, onMounted } from "vue"

import labelList from "@/pages/register/components/personalLabel/index.vue"
import { getAuthorization, setAuthorization } from "@/utils/auth.js"

import {
    UerAllTags, // 更新tag
} from '@/api/register/index.js'

let { proxy } = getCurrentInstance()

const state = reactive({
    isLoading:false,
    tagsList:[],
    checkLabel: {
        "value": [
            { "state": 0, "tagGroupCode": "JYPH", "tags": [] },
            { "state": 0, "tagGroupCode": "ZWMS", "tags": [] },
            { "state": 0, "tagGroupCode": "XQSC", "tags": [] }
        ]
    }
})

let PersonalLabelRef = ref()
const nextFn = async () => {
    PersonalLabelRef.value.updateUserTag()
    let JYPH = state.checkLabel.value[0].tags.length
    let ZWMS = state.checkLabel.value[1].tags.length
    let XQSC = state.checkLabel.value[2].tags.length
    if (JYPH == 0 || ZWMS == 0 || XQSC == 0) {
        uni.showToast({
            title: '请选择标签',
            icon: "none"
        })
        return
    }
    state.isLoading = true
    try {
        let data = await proxy.$put(UerAllTags(), state.checkLabel)
        state.isLoading = false
        uni.navigateTo({
            url:"/pages/home/person/profile/index"
        })
    } catch (error) {
        state.isLoading = false
    }
}

onMounted(async () => {
    let tagsList = JSON.parse(getAuthorization("userInfo"))['Tags'].split(',')
    state.tagsList = tagsList
})



</script>

<style lang="scss" scoped>
.label-main {
    padding: 30rpx;
    box-sizing: border-box;
}

.van-button {
    width: 100%;
    margin-top: 60rpx;
    font-size: 36rpx;
    height: 100rpx;
    line-height: 100rpx;
}
</style>
